<template>
  <div class="login-container">
    <div class="login-background">
      <!-- 动态背景 -->
      <div class="cyber-grid"></div>
      <div class="particles"></div>
    </div>
    
    <div class="login-box">
      <div class="login-header">
        <el-icon :size="50" class="logo-icon"><Platform /></el-icon>
        <h1 class="login-title neon-text">CMDB资产管理系统</h1>
        <p class="login-subtitle">Cyber Asset Management Platform</p>
      </div>

      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名"
            size="large"
            clearable
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password"
            placeholder="密码"
            size="large"
            show-password
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button 
            type="primary" 
            size="large" 
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            <el-icon v-if="!loading"><Right /></el-icon>
            {{ loading ? '登录中...' : '登 录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <el-button type="text" @click="$router.push('/register')">
          还没有账号？立即注册
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { authAPI } from '@/api'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      
      try {
        const response = await authAPI.login({
          username: loginForm.username,
          password: loginForm.password
        })
        
        // 保存登录状态和token
        localStorage.setItem('isLoggedIn', 'true')
        localStorage.setItem('username', response.user.username)
        localStorage.setItem('access_token', response.tokens.access)
        localStorage.setItem('refresh_token', response.tokens.refresh)
        
        if (loginForm.remember) {
          localStorage.setItem('remember', 'true')
        }
        
        ElMessage.success(response.message || '登录成功！')
        
        // 跳转到仪表盘
        router.push('/dashboard')
      } catch (error) {
        console.error('登录失败:', error)
        const errorMsg = error.response?.data?.error || '登录失败，请检查用户名和密码'
        ElMessage.error(errorMsg)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #0a0e27 0%, #151a35 50%, #0a0e27 100%);
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.cyber-grid {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: 
    linear-gradient(rgba(0, 255, 159, 0.1) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 255, 159, 0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: grid-move 20s linear infinite;
}

@keyframes grid-move {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(50px, 50px);
  }
}

.particles {
  position: absolute;
  width: 100%;
  height: 100%;
}

.login-box {
  position: relative;
  z-index: 1;
  width: 450px;
  padding: 50px 40px;
  background: linear-gradient(135deg, var(--cyber-bg-secondary) 0%, #1a2040 100%);
  border: 2px solid var(--cyber-primary);
  border-radius: 20px;
  box-shadow: 0 0 50px rgba(0, 255, 159, 0.5);
  backdrop-filter: blur(10px);
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-icon {
  color: var(--cyber-primary);
  animation: neon-pulse 2s ease-in-out infinite;
}

.login-title {
  font-size: 28px;
  margin: 20px 0 10px;
  color: var(--cyber-primary);
  font-family: 'Courier New', monospace;
  letter-spacing: 2px;
}

.login-subtitle {
  font-size: 12px;
  color: var(--cyber-text-dim);
  letter-spacing: 3px;
  text-transform: uppercase;
}

.login-form {
  margin-top: 30px;
}

.login-form :deep(.el-form-item) {
  margin-bottom: 25px;
}

.login-form :deep(.el-input__wrapper) {
  background: rgba(10, 14, 39, 0.5) !important;
  border: 2px solid rgba(0, 255, 159, 0.3);
  transition: all 0.3s;
}

.login-form :deep(.el-input__wrapper:hover),
.login-form :deep(.el-input__wrapper.is-focus) {
  border-color: var(--cyber-primary);
  box-shadow: 0 0 15px rgba(0, 255, 159, 0.5);
}

.login-form :deep(.el-checkbox__label) {
  color: var(--cyber-text);
}

.login-button {
  width: 100%;
  height: 50px;
  font-size: 16px;
  letter-spacing: 5px;
  margin-top: 10px;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
}

.login-footer :deep(.el-button) {
  color: var(--cyber-secondary);
  font-size: 14px;
}

.login-footer :deep(.el-button:hover) {
  color: var(--cyber-primary);
}
</style>
